package com.example.teacherservice.service;

import com.example.teacherservice.dto.PracticeSelectionDTO;
import com.example.teacherservice.dto.StudentPracticeDTO;
import com.example.teacherservice.entity.Practice;
import com.example.teacherservice.repository.CourseRepository;
import com.example.teacherservice.repository.PracticeRepository;
import com.example.teacherservice.repository.StuPracticeRepository;
import com.example.teacherservice.repository.TeacherRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

// PracticeStudentService.java
@Service
@RequiredArgsConstructor
public class PracticeStudentService {
    private final PracticeRepository practiceRepository;
    private final StuPracticeRepository stuPracticeRepository;
    private final TeacherRepository teacherRepository;
    private final CourseRepository courseRepository;

    // 学生查看所有可加入的实践活动
    public List<PracticeSelectionDTO> getAllPracticesForStudent(Long studentId) {
        // 获取所有未删除的实践活动
        List<Practice> practices = practiceRepository.findByDeleted(0);

        return practices.stream().map(p -> {
            PracticeSelectionDTO dto = new PracticeSelectionDTO();
            dto.setId(p.getId());
            dto.setName(p.getName());
            dto.setPraName(p.getPraName());
            dto.setDescription(p.getDescription());
            dto.setLocation(p.getLocation());
            dto.setStartTime(p.getStartTime());
            dto.setEndTime(p.getEndTime());
            dto.setStatus(p.getStatus());

            // 设置教师和课程信息
            teacherRepository.findById(p.getTeaId()).ifPresent(t ->
                    dto.setTeacherName(t.getTeaName()));
            courseRepository.findById(p.getCouId()).ifPresent(c ->
                    dto.setCourseName(c.getCouName()));

            // 检查是否已加入
            dto.setIsJoined(stuPracticeRepository.existsByStudentAndPractice(studentId, p.getId()));

            return dto;
        }).collect(Collectors.toList());
    }

    // 学生加入实践活动
    @Transactional
    public void joinPractice(Long studentId, Long practiceId) {

        // 检查是否已加入
        if (stuPracticeRepository.existsByStudentAndPractice(studentId, practiceId)) {
            throw new RuntimeException("您已加入该实践活动");
        }

        // 加入实践
        int result = stuPracticeRepository.joinPractice(studentId, practiceId);
        if (result == 0) {
            throw new RuntimeException("加入实践活动失败");
        }
    }

    // 学生退出实践活动
    @Transactional
    public void quitPractice(Long studentId, Long practiceId) {
        // 可选：先检查是否存在关联记录
        if (!stuPracticeRepository.existsByStudentIdAndPracticeId(studentId, practiceId)) {
            throw new RuntimeException("未找到该学生的实践活动记录");
        }

        int result = stuPracticeRepository.quitPractice(studentId, practiceId);
        if (result == 0) {
            throw new RuntimeException("退出实践活动失败");
        }
    }

    // 学生查看自己已加入的实践活动
    public List<StudentPracticeDTO> getJoinedPractices(Long studentId) {
        return stuPracticeRepository.findPracticesByStudentId(studentId);
    }
}
